package com.lnlyj.media.widget;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import static com.lnlyj.media.base.BaseGroupAdapter.TYPE_GROUP;

/**
 * Created by Wanglei on 2019/6/26.
 */

public class SpaceItemDecoration extends RecyclerView.ItemDecoration {

    private int space;

    public SpaceItemDecoration(int space) {
        this.space = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

        RecyclerView.LayoutManager manager = parent.getLayoutManager();

        if (manager != null) {
            if (manager.getClass() == GridLayoutManager.class) {
                //分别设置item的间距
                if (parent.getChildViewHolder(view).getItemViewType() == TYPE_GROUP) {
                    outRect.bottom = 0;
                    outRect.top = 0;
                    outRect.right = 0;
                    outRect.left = 0;
                } else {
                    outRect.bottom = space;
                    outRect.top = space;
                    outRect.right = space;
                    outRect.left = space;
                }
            } else if (manager.getClass() == LinearLayoutManager.class) {

                if (((LinearLayoutManager) manager).getOrientation() == RecyclerView.HORIZONTAL) {
                    outRect.bottom = 0;
                    outRect.top = 0;
                    outRect.right = space * 2;
                    outRect.left = space * 2;
                } else {
                    outRect.bottom = space * 2;
                    outRect.top = space * 2;
                    outRect.right = 0;
                    outRect.left = 0;
                }
            }
        }
    }
}
